package com.wtf.dsp.admin.controller;


import com.baomidou.mybatisplus.annotation.DbType;
import com.wtf.dsp.admin.entity.WtfDspDbConfig;
import com.wtf.dsp.admin.model.dto.*;
import com.wtf.dsp.admin.model.vo.ColumnVO;
import com.wtf.dsp.admin.model.vo.PreviewCodeVO;
import com.wtf.dsp.admin.service.DataBaseSerivce;
import com.wtf.dsp.core.api.R;
import com.wtf.dsp.core.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
@Api(tags = "数据库")
@RestController
@RequestMapping("/db")
public class DataBaseController {

    @Autowired
    private DataBaseSerivce dataBaseSerivce;

    @ApiOperation(value = "获取数据库表")
    @GetMapping("/table/page")
    public R page(TablePageDTO tableDTO) throws Exception {
        PageUtils pageUtils = dataBaseSerivce.tablePage(tableDTO);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "获取数据库表字段列")
    @GetMapping("/table/columns")
    public R columns(ColumsDTO ColumsDTO) throws Exception {
        List<ColumnVO> columns = dataBaseSerivce.columns(ColumsDTO);
        return R.ok(columns);
    }

    @ApiOperation(value = "预览代码")
    @PostMapping("/table/preview")
    public R preview(@RequestBody @Validated CodeDTO codeDTO) throws Exception {
        List<PreviewCodeVO> dataMap = dataBaseSerivce.preview(codeDTO);
        return R.ok(dataMap);
    }

    @ApiOperation(value = "生成代码")
    @PostMapping("/table/code")
    public void generatorCode(@RequestBody @Validated CodeDTO codeDTO, HttpServletResponse response) throws Exception {
        byte[] data = dataBaseSerivce.generatorCode(codeDTO);
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"wtf.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        //response.setContentType("application/zip; charset=UTF-8");
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

    @ApiOperation(value = "数据库配置分页")
    @GetMapping("/config/page")
    public R page(DbConfigPageDTO dbConfigPageDTO){
        PageUtils pageUtils = dataBaseSerivce.dbConfigPage(dbConfigPageDTO);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "数据库配置")
    @GetMapping("/config/list")
    public R list(){
        List<DbType> result = dataBaseSerivce.dbConfigList();
        return R.ok(result);
    }

    @ApiOperation(value = "数据库类型")
    @GetMapping("/config/dbType")
    public R dbType(){
        List<Map<String, Object>> result = Arrays.stream(DbType.values()).map(e -> {
            Map<String, Object> map = new HashMap<>();
            map.put("dbType", e);
            map.put("desc", e.getDesc());
            return map;
        }).collect(Collectors.toList());
        return R.ok(result);
    }

    @ApiOperation(value = "新增数据库配置")
    @PostMapping("/config/add")
    public R addDbConfig(@RequestBody @Validated DbConfigAddDTO dbConfigAddDTO){
        int save = dataBaseSerivce.addDbConfig(dbConfigAddDTO);
        return R.ok(save);
    }

    @ApiOperation(value = "删除数据库配置")
    @PostMapping("/config/update")
    public R updateDbConfig(@RequestBody @Validated DbConfigUpdateDTO dbConfigUpdateDTO){
        int update = dataBaseSerivce.updateDbConfig(dbConfigUpdateDTO);
        return R.ok(update);
    }

    @ApiOperation(value = "删除数据库配置")
    @PostMapping("/config/del")
    public R removeDbConfig(@RequestBody @Validated DbConfigDelDTO dbConfigDelDTO){
        int del = dataBaseSerivce.removeDbConfig(dbConfigDelDTO);
        return R.ok(del);
    }

    @ApiOperation(value = "数据库驱动测试")
    @PostMapping("/config/driver/test")
    public R driverClassTest(@RequestBody @Validated DbConfigDriverTestDTO dbConfigDriverTestDTO){
        boolean result = dataBaseSerivce.driverClassTest(dbConfigDriverTestDTO);
        return R.ok(result);
    }
}
